﻿using WoodAdmin.Common.Helper.Net;
using WoodAdmin.Models;
using System;
using System.Diagnostics;

namespace WoodAdmin.Common.Helper.Log
{
    public class ExecuteSqlLogHandler : LogHandler<ExecuteSqlLog>
    {
        Stopwatch stopwatch = new Stopwatch();
        public ExecuteSqlLogHandler(string sql, string parm) : base(LogMode.SqlLog)
        {
            SysUser currentUser = null;
            var current = HttpContextHelper.Current;
            if (current != null)
            {
                currentUser = GlobalHelper.CurrentUser();
            }
            if (currentUser == null)
            {
                currentUser = new SysUser()
                {
                    NickName = "匿名用户"
                };
            }
            LogInfo = new ExecuteSqlLog()
            {
                SqlCommand = sql,
                Parameter = parm,
                CreateAccountId = currentUser?.ID.ToString(),
                CreateUserName = currentUser?.NickName,
                CreatorTime = DateTime.Now
            };
            stopwatch.Start();
        }

        public override void WriteLog()
        {
            stopwatch.Stop();
            LogInfo.ElapsedTime = stopwatch.Elapsed.TotalSeconds;
            base.WriteLog();
        }
    }
}
